package ge.telasi.tasks.ui.user;

import ge.telasi.tasks.model.User;
import ge.telasi.tasks.ui.Platform;
import ge.telasi.tasks.ui.UIUtils;
import ge.telasi.tasks.ui.comp.CommonDialog;
import ge.telasi.tasks.ui.log.LoggerUtils;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

/**
 * @author dimitri
 */
public class UserRelationViewDialog  extends CommonDialog {
    private UserTableBase tblBosses;
    private UserTableBase tblAssistants;
    private JButton btnClose;
    private User user;

    public UserRelationViewDialog(Component parent) {
        super(parent, true);
        initComponents();
        setSize(700, 300);
    }

    private void initComponents() {
        getContentPane().setLayout(new BorderLayout(5, 5));
        JPanel mainPanel = new JPanel(new BorderLayout(5, 5));
        mainPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        mainPanel.add(createCenterPanel(), BorderLayout.CENTER);
        mainPanel.add(createBottomPanel(), BorderLayout.SOUTH);
        getContentPane().add(mainPanel, BorderLayout.CENTER);
        getRootPane().setDefaultButton(btnClose);
    }

    private JComponent createCenterPanel() {
        JPanel centerPanel = new JPanel(new GridLayout(1, 2, 5, 5));
        centerPanel.add(createLeftComponent());
        centerPanel.add(createRightComponent());
        return centerPanel;
    }

    private JComponent createLeftComponent() {
        JPanel panel = new JPanel(new BorderLayout(0, 0));
        tblBosses = new UserTableBase();
        tblBosses.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        panel.add(new JScrollPane(tblBosses), BorderLayout.CENTER);
        JLabel label = new JLabel("უფროსები:");
        label.setFont(UIUtils.makeBold(label.getFont()));
        panel.add(label, BorderLayout.NORTH);
        return panel;
    }

    private JComponent createRightComponent() {
        JPanel panel = new JPanel(new BorderLayout(0, 0));
        tblAssistants = new UserTableBase();
        tblAssistants.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        panel.add(new JScrollPane(tblAssistants), BorderLayout.CENTER);
        JLabel label = new JLabel("ასისტენტები:");
        label.setFont(UIUtils.makeBold(label.getFont()));
        panel.add(label, BorderLayout.NORTH);
        return panel;
    }

    private JComponent createBottomPanel() {
        JPanel panel = new JPanel(new BorderLayout());
        btnClose = new JButton("გასვლა");
        panel.add(btnClose, BorderLayout.EAST);
        btnClose.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });
        return panel;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
        setTitle(user.getFullName() + " (" + user.getUsername() + ") -- დამოკიდებულება");
    }

    public boolean refresh() {
        Platform pl = Platform.getDefault();
        if (pl.validateConnection()) {
            try {
                user = pl.getFacade().getUserWithRelations(pl.getCredentials(), user);
                tblBosses.display(user.getBosses());
                tblAssistants.display(user.getAssistants());
                return true;
            } catch (Exception ex) {
                LoggerUtils.manageException(this, ex);
            }
        }
        return false;
    }

}
